﻿package  {
	
	import flash.display.MovieClip;	
	import flash.geom.Rectangle;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.display.BitmapData;
	import flash.ui.Keyboard;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.geom.Matrix;
	import flash.text.Font;
	
	public class RunningGame extends BaseGame {

		private var FPSLabel:TextField = new TextField();	
		private var healthLabel:TextField = new TextField();
		private var healthMat:Matrix = new Matrix();		
		private var scoreLabel:TextField = new TextField();
		private var scoreMat:Matrix = new Matrix();		

		private var bgPos1:Point;
		private var bgPos2:Point;
		protected var bgVelocity:int;
		private var backgroundSprite:GameSprite;
		
		protected var pointBaseSprite:GameSprite;
		protected var pointBasePos:Point;
		
		private var player:Player;
		private var obstacleController:ObstacleController;

		public function RunningGame(displayContainer : MovieClip) {
			super(displayContainer);				
						
			healthMat.translate(0, 10);				
			scoreMat.translate(0,600);			
			pointBasePos = new Point(0, 570);			
			
		}
		
		override public function initialize() {
			super.initialize();
			
			bgVelocity = ObstacleController.obstacleVelocity;
			
			bgPos1 = new Point(0, 0);			
		}
		
		override public function loadContent() {
			super.loadContent();
			backgroundSprite = new GameSprite(new BackgroundBD(), new Point(0,0));
			pointBaseSprite = new GameSprite(new PointBaseBD(), new Point(0,0));						
			var playerSprite : GameSprite = new GameSprite(new PlayerBD());
						
			player = new Player(playerSprite);
			playerSprite.origin = new Point(player.getWidth() / 2, player.getHeight() / 2);			
			player.initialize();
			player.pos = new Point(screenWidth / 2, screenHeight - player.getHeight()/2 + 100) ;
			obstacleController = new ObstacleController(this);
			
			bgPos2 = new Point(0, -backgroundSprite.bitmapData.height);
		}
		
		override public function unloadContent() {
			super.unloadContent();			
		}
		
		override public function update(elapsedTime:Number) {
			super.update(elapsedTime);
			
			updateBackground(elapsedTime);
			updatePlayer(elapsedTime);			
			obstacleController.update(elapsedTime,intersectWithPlayer, player); //Parameter kedua = fungsi apa aja, dieksekusi pas update obstacleController buat dapetin obstacle
		}
		
		protected function updateBackground(elapsedTime:Number) {
			var delta = elapsedTime * bgVelocity;
			bgPos1.y += delta;
			bgPos2.y += delta;			
			if(bgPos1.y > screenHeight) {
				bgPos1.y = bgPos2.y-backgroundSprite.bitmapData.height;								
			}
			if(bgPos2.y > screenHeight) {
				bgPos2.y = bgPos1.y-backgroundSprite.bitmapData.height;																
			}			
		}
		
		private function intersectWithPlayer(obstacle:Obstacle, player:Player) {					
			if(obstacle.getType()==Obstacle.HAZARD) {			
				player.health--;
				player.blinkIn(100,1000);
			}else if(obstacle.getType()==Obstacle.COIN) {
				player.addScore(100);
			}
		}
		
		protected function updatePlayer(elapsedTime:Number) {			
			if(input.isKeyPressed(Keyboard.RIGHT)) {
				player.velocity.x = 400;				
			}else if(input.isKeyPressed(Keyboard.LEFT)) {
				player.velocity.x = -400;
			}else{
				player.velocity.x = 0;
			}
			
			if(input.isKeyPressed(Keyboard.DOWN)) {
				player.velocity.y = 400;				
			}else if(input.isKeyPressed(Keyboard.UP)) {
				player.velocity.y = -400;
			}else{
				player.velocity.y = 0;
			}
			
									
			player.update(elapsedTime);
						
			if(player.pos.x > (screenWidth - (player.getWidth() / 2.0)))
				player.pos.x = (screenWidth - (player.getWidth() / 2.0));
			else if(player.pos.x < player.getWidth() / 2.0)
				player.pos.x = player.getWidth() / 2.0;
			
			if(player.pos.y > (screenHeight - (player.getHeight() / 2.0) + 200)) {
				player.pos.y = (screenHeight - (player.getHeight() / 2.0) + 200);				
			}else if(player.pos.y < player.getHeight() / 2){
				player.pos.y = player.getHeight() / 2.0;
			}
		}
		
		protected function handleLose(player:Player) {
								
		}
		
		override public function render(elapsedTime:Number) {
			super.render(elapsedTime);
			
			canvasBD.lock();			
			canvasBD.fillRect(new Rectangle(0,0,screenWidth, screenHeight), 0x000000);
			drawBackground(canvasBD);
			obstacleController.render(canvasBD, elapsedTime);
			player.render(canvasBD, elapsedTime);		
			drawHUD(canvasBD);
			
			canvasBD.unlock();
		}
		
		protected function drawBackground(canvasBD:BitmapData) {
			canvasBD.copyPixels(backgroundSprite.bitmapData, new Rectangle(0,0,backgroundSprite.bitmapData.width, backgroundSprite.bitmapData.height), bgPos2);
			canvasBD.copyPixels(backgroundSprite.bitmapData, new Rectangle(0,0,backgroundSprite.bitmapData.width, backgroundSprite.bitmapData.height), bgPos1);
		}		
		
		protected function drawHUD(canvasBD:BitmapData) {
			canvasBD.copyPixels(pointBaseSprite.bitmapData, new Rectangle(0,0,pointBaseSprite.bitmapData.width, pointBaseSprite.bitmapData.height), pointBasePos);			
			
			FPSLabel.text = "FPS : "+frameRate;
			canvasBD.draw(FPSLabel);		
			healthLabel.text = "health : " + player.health;				
			canvasBD.draw(healthLabel, healthMat)
			scoreLabel.text = ""+player.getScore();
			canvasBD.draw(scoreLabel, scoreMat);
			
		}
	}	
}
